package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.NewsCommentQueryObject;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsCommentService newsCommentService;
   @Autowired
   private IStrategyService strategyService;
   @Autowired
   private IStrategyCatalogService strategyCatalogService;
   @Autowired
   private IStrategyDetailService strategyDetailService;

    /*
     * 获取所有已发布的日报
     * 资源:/news
     * 动作:GET
     * 参数:isRelease=1
     * */
    @GetMapping
    public Object getIsRelease(NewsQueryObject qo){
        return newsService.query(qo);
    }

    /*
    * 根据攻略的id查询日报
    * 资源:/news/{id}
    * 动作:get
    * 参数:
    * */
    @GetMapping("/{id}")
    public Object getNewsById(@PathVariable Long id){
        return newsService.get(id);
    }

    /*
     * 根据日报的类型id查询日报(不包括原来的日报)
     * 资源:/news/commends/{typeId}
     * 动作:get
     * 参数:id=x(原日报的id)
     * */
    @GetMapping("/commends/{typeId}")
    public Object getNewsByTypeId(@PathVariable Long typeId, Long id){
        return newsService.getNewsByTypeId(typeId, id);
    }

    /*
     * 根据日报的id修改状态state为收藏/未收藏,并新增或删除收藏
     * 资源:/news/{id}
     * 动作:PUT
     * 参数:state=1/0 & userId=x
     * */
    @PutMapping("/{id}")
    public Object updateStateById(@PathVariable Long id, Integer state, Long userId){
        newsService.updateStateById(id, state, userId);
        return new JSONResult();
    }

    /*
     * 根据某个大攻略下的攻略分类
     * 资源:/news/{id}/catalogs
     * 动作:get
     * 参数:
     * */
    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogsByStrateById(@PathVariable Long strategyId){
        return strategyCatalogService.selectByStrategyId(strategyId);
    }

    /*
     * 根据某个攻略下的文章内容
     * 资源:/news/details/{id}
     * 动作:get
     * 参数:
     * */
    @GetMapping("/details/{id}")
    public Object getDetailsById(@PathVariable Long id){
        return strategyDetailService.get(id);
    }

    //添加一条评论
    @PostMapping("/newscomment/{newsId}")
    public Object saveNewsComment(String content,Long userId,@PathVariable Long newsId){
        JSONResult jsonResult = new JSONResult();
        News news = new News();
        NewsComment newsComment = new NewsComment();
        news.setId(newsId);
        newsComment.setNews(news);
        User user = new User();
        user.setId(userId);
        newsComment.setUser(user);
        newsComment.setContent(content);
        try {
            newsCommentService.saveComment(newsComment);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //查询一条点评中所有的评论,并且回显在页面
    @GetMapping("/{newsId}/comment")
    public Object getCommentreplay(NewsCommentQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo = newsCommentService.selectForList(qo);
        return pageInfo;
    }
}
